package com.study.zzedu.ui.login;

import android.content.Context;
import android.os.Handler;
import android.util.Log;

import com.study.zzedu.base.BaseModel;
import com.study.zzedu.http.OnResultListener;
import com.study.zzedu.utils.LogUtil;
import com.study.zzedu.utils.OkHttpUtils;

import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.HttpVersion;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;


/**
 * Created by kiros on 2019/7/4.
 */

public class LoginModel extends BaseModel {

    public void getImageCode(Context context, final OnResultListener resultListener) {
        OkHttpUtils.getInstance(context).getJsonForString(OkHttpUtils.BASE_URL + "/code/getImageCode",
                null, null, new OkHttpUtils.HttpCallBack() {
                    @Override
                    public void onSuccess(String obj) {
                        try {
                            JSONObject jObject = new JSONObject(obj);
                            if (0 == jObject.getInt("code")) {

                                resultListener.onSuccess(obj);
                            } else {
                                resultListener.onError(jObject.get("message").toString());
                            }
                        } catch (Exception e) {
                            e.toString();
                        }
                    }

                    @Override
                    public void onSuccess(JSONObject obj) {
                        resultListener.onSuccess(obj.toString());
                    }

                    @Override
                    public void onError(String msg) {
                        resultListener.onError(msg);
                    }
                });
    }

    public void getLoginToken(Context context, final OnResultListener resultListener) {
        OkHttpUtils.getInstance(context).getJsonForString(OkHttpUtils.BASE_URL + "/code/getLoginToken",
                null, null, new OkHttpUtils.HttpCallBack() {
                    @Override
                    public void onSuccess(String obj) {
                        try {
                            JSONObject jObject = new JSONObject(obj);
                            if (0 == jObject.getInt("code")) {
                                resultListener.onSuccess(obj);
                            } else {
                                resultListener.onError(jObject.get("message").toString());
                            }
                        } catch (Exception e) {
                            e.toString();
                        }
                    }

                    @Override
                    public void onSuccess(JSONObject obj) {
                        resultListener.onSuccess(obj.toString());
                    }

                    @Override
                    public void onError(String msg) {
                        resultListener.onError(msg);
                    }
                });
    }

    public void login(Context context, final String username, final String password, final String code, final String uuid,String deviceId,final OnResultListener resultListener) {
        OkHttpUtils.getInstance(context).getJsonForString(OkHttpUtils.BASE_URL + "/code/userLogin",
                new String[]{"username", "password", "code","codeKey","deviceId"}, new String[]{username, password, code,uuid,deviceId},new OkHttpUtils.HttpCallBack() {
                    @Override
                    public void onSuccess(String obj) {
                        try {
                            JSONObject jObject = new JSONObject(obj);
                            if (0 == jObject.getInt("code")) {
                                resultListener.onSuccess(obj);
                            } else {
                                resultListener.onError(jObject.get("message").toString());
                            }
                        } catch (Exception e) {
                            resultListener.onSuccess(e.toString());
                        }
                    }

                    @Override
                    public void onSuccess(JSONObject obj) {
                        resultListener.onSuccess(obj.toString());
                    }

                    @Override
                    public void onError(String msg) {
                        resultListener.onError(msg);
                    }
                });
    }
    public void autoLogin(Context context, final String username, final String password, final String deviceId, final OnResultListener resultListener) {
        OkHttpUtils.getInstance(context).getJsonForString(OkHttpUtils.BASE_URL + "/code/phoneUserLogin",
                new String[]{"username", "password", "deviceId"}, new String[]{username, password, deviceId},new OkHttpUtils.HttpCallBack() {
                    @Override
                    public void onSuccess(String obj) {
                        try {
                            JSONObject jObject = new JSONObject(obj);
                            if (0 == jObject.getInt("code")) {
                                resultListener.onSuccess(obj);
                            } else {
                                resultListener.onError(jObject.get("message").toString());
                            }
                        } catch (Exception e) {
                            resultListener.onSuccess(e.toString());
                        }
                    }

                    @Override
                    public void onSuccess(JSONObject obj) {
                        resultListener.onSuccess(obj.toString());
                    }

                    @Override
                    public void onError(String msg) {
                        resultListener.onError(msg);
                    }
                });
    }

    public void login2(Context context, String username, String password, String code, String uuid, Handler handler, final OnResultListener resultListener) {
        JSONObject job = new JSONObject();

        try {
            job.put("username",username);
            job.put("password",password);
            job.put("code",code);
            job.put("codeKey",uuid);
        }catch (Exception e){
            e.toString();
        }
        OkHttpUtils.getInstance(context).postJson(OkHttpUtils.BASE_URL + "/code/userLogin",
               job.toString(),new OkHttpUtils.HttpCallBack() {
                    @Override
                    public void onSuccess(String obj) {
                        try {
                            JSONObject jObject = new JSONObject(obj);
                            if (0 == jObject.getInt("code")) {
                                resultListener.onSuccess(obj);
                            } else {
                                resultListener.onError(jObject.get("message").toString());
                            }
                        } catch (Exception e) {
                            resultListener.onSuccess(e.toString());
                        }
                    }

                    @Override
                    public void onSuccess(JSONObject obj) {
                        resultListener.onSuccess(obj.toString());
                    }

                    @Override
                    public void onError(String msg) {
                        resultListener.onError(msg);
                    }
                });
    }


    public String doGet(String url,String user,String pwd,String code2 ,String token) {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        // 使用系统提供的默认的恢复策略
        InputStream in = null;
        BufferedReader br = null;
        try {
            // 执行getMethod
            HttpGet method = new HttpGet(url);
            List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>();
            params.add(new BasicNameValuePair("username",user));
            params.add(new BasicNameValuePair("password",pwd));
            params.add(new BasicNameValuePair("code",code2));
            params.add(new BasicNameValuePair("codeKey",token));
            //对参数编码
            String param = URLEncodedUtils.format(params, "UTF-8");
            //将URL与参数拼接
            HttpGet getMethod = new HttpGet(url + "?" + param);
            try {
                HttpResponse response = httpClient.execute(getMethod); //发起GET请求
                LogUtil.i("player", "resCode = " + response.getStatusLine().getStatusCode()); //获取响应码
                LogUtil.i("player", "result = " + EntityUtils.toString(response.getEntity(), "utf-8"));//获取服务器响应内容
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return "st";
        } catch (Exception e) {

            // 发生网络异常
            e.printStackTrace();
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
                if (in != null) {
                    in.close();
                }

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

        // 释放连接   getMethod.releaseConnection();  }
        return "";
    }
}

